package com.badshun.weapon;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badshun.box2d.Box2DWorld;
import com.badshun.player.MainPlayer;

public class Sword {
	private Sprite leftSword;
	private Sprite rightSword;
	private Sprite upSword;
	private Sprite downSword;
	
	public Sprite mainSword;

	private OrthographicCamera camera;
	private MainPlayer player;
	private Box2DWorld b2dWorld;
	private Vector2 position;
	
	private SpriteBatch batch;
	
	public Sword(OrthographicCamera camera, MainPlayer player, Box2DWorld b2dWorld) {
		this.camera = camera;
		this.player = player;
		this.b2dWorld = b2dWorld;
		
		leftSword = new Sprite(new Texture(Gdx.files.internal("weapon/sword.png")));
		leftSword.setSize(leftSword.getWidth(), leftSword.getHeight());
		leftSword.setOrigin(leftSword.getWidth(), leftSword.getHeight() / 2);
		leftSword.setRotation(-45);
		
		rightSword = new Sprite(new Texture(Gdx.files.internal("weapon/sword.png")));
		rightSword.setSize(rightSword.getWidth(), rightSword.getHeight());
		rightSword.setOrigin(0, rightSword.getHeight() / 2);
		rightSword.flip(true, false);
		rightSword.setRotation(45);
		
		upSword = new Sprite(new Texture(Gdx.files.internal("weapon/sword.png")));
		upSword.setSize(upSword.getWidth(), upSword.getHeight());
		upSword.setOrigin(0, upSword.getHeight() / 2);
		upSword.flip(true,  false);
		upSword.setRotation(135);
		
		downSword = new Sprite(new Texture(Gdx.files.internal("weapon/sword.png")));
		downSword.setSize(downSword.getWidth(), downSword.getHeight());
		downSword.setOrigin(downSword.getWidth(), downSword.getHeight() / 2);
		downSword.setRotation(45);
		
		batch = new SpriteBatch();
	}
	
	private void autoSetMainSword() {
		switch (player.direction) {
			case LEFT:
				mainSword = leftSword;
				mainSword.setPosition(position.x - player.keyFrame.getRegionWidth() / 2 - mainSword.getWidth() / 2,  position.y);
				
				break;
			case RIGHT:
				mainSword = rightSword;
				mainSword.setPosition(position.x + player.keyFrame.getRegionWidth() / 2, position.y);
				
				break;
			case UP:
				mainSword = upSword;
				mainSword.setPosition(position.x + player.keyFrame.getRegionWidth() / 2, position.y + player.keyFrame.getRegionWidth() / 2);
				
				break;
			case DOWN:
				mainSword = downSword;
				mainSword.setPosition(position.x - downSword.getWidth() / 2 - player.keyFrame.getRegionWidth() / 2, position.y - player.keyFrame.getRegionWidth() / 2);
				
				break;
		}
	}
	
	public void render() {
		position = player.position;
		
		autoSetMainSword();
		
		batch.begin();
		batch.setProjectionMatrix(camera.combined);
		mainSword.draw(batch);
		batch.end();
	}
}





















